<?php
	/*****************************************************************
		GP Clinic - a web based Medical directory system
		Copyright (C) 2014 Kaizensynergy - Gemba360

		Author: Bless Bauson
		Key: bHkTy89wZ0DHyUiim0041rTxyBbP671aQwg327UtjfcrO
	*****************************************************************/
	 
	$add = fetchAddFormParameters();
	
	//search filter submit
	$obj = new User();
	if($_SERVER['REQUEST_METHOD'] == "POST"){

		if(isset($_POST['inactive']) && $_POST['inactive'] == 1){
			$obj->filter = "user.active <= 1";
		}else{
			$obj->filter = "user.active = 1";
		}

		if(isset($_POST['searchby']) && !empty($_POST['searchby']) && !empty($_POST['keyword'])){
			switch($_POST['searchby']){
				case "name":
					$obj->SQL_AddFilter("up.name LIKE '%".$_POST['keyword']."%' OR unp.name LIKE '%".$_POST['keyword']."%' OR user.username LIKE '%".$_POST['keyword']."%' ");
					break;
				case "label":
					$obj->SQL_AddFilter("ut.label LIKE '%".$_POST['keyword']."%'");
					break;
			}
		}
	}

	//view all using pagination
	$obj->SQL_AddJoin("INNER JOIN", "user_type AS ut", "user.user_type_id = ut.id", "ut.label AS user_type_label");
	$obj->SQL_AddJoin("LEFT JOIN", "user_practicioner AS up", "user.id = up.user_id", "up.name AS prac_name, up.address, up.phone, up.presciber_no, up.provider_no");
	$obj->SQL_AddJoin("LEFT JOIN", "user_non_practicioner AS unp", "user.id = unp.user_id", "unp.name AS non_prac_name");
	
	//if not super administrator, filter results to all users belonging to the same group
	if($_SESSION['account']['user_type_id'] != CONST_SUPER_ADMIN_TYPE){
		$obj->SQL_AddJoin("LEFT JOIN", "user_group AS ug", "user.id = ug.user_id", "ug.group_id");
		
		if(!empty($_SESSION['account']['group_id'])){
			$obj->SQL_AddFilter("ug.group_id = ".$_SESSION['account']['group_id']);
		}
	}

	$sql 		= $obj->SQL_FormQuery();
	$cntrows	= $obj->SQL_Count();

	//form pagination
	$rowcntPerPage  = CONST_PAGE_ROW_COUNT;
	$userObj = new Pagination(
				$sql,				//query resultset
				$rowcntPerPage,		//max # of rows displayed
				'page',				//pagination variable used in GET or POST
				5					//number of links displayed in getPageNav
			);
?>